package com.yoshino.principle.demeter.improve;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yoshino
 **/
public class Demeter1 {
    public static void main(String[] args) {
        // 创建一个学校管理对象
        SchoolManager schoolManager = new SchoolManager();
        // 输出学院的员工 id 和学校总部的员工信息
        schoolManager.printAllEmployee(new CollegeManager());
    }
}

@Data
class Employee {
    private String id;

}

@Data
class CollegeEmployee {
    private String id;
}

class CollegeManager {
    public List<CollegeEmployee> getAllEmployee() {
        List<CollegeEmployee> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            CollegeEmployee emp = new CollegeEmployee();
            emp.setId("学院员工id=" + i);
            list.add(emp);
        }
        return list;
    }
}

//分析 SchoolManager 类的直接朋友类有哪些 Employee、CollegeManager、
// CollegeEmployee 不是直接朋友，违背了 迪米特法则

@Slf4j
class SchoolManager {
    public List<Employee> getAllEmployee() {
        List<Employee> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Employee emp = new Employee();
            emp.setId("学校总部员工id=" + i);
            list.add(emp);
        }
        return list;
    }

    void printAllEmployee(CollegeManager sub) {
        // 分析问题
        // 1.这里的 CollegeEmployee 不是 SchoolManager 的直接朋友
        // 2.CollegeEmployee 是以局部变量方式在 SchoolManager
        // 3.违反了 迪米特法则
        List<CollegeEmployee> list1 = sub.getAllEmployee();
        log.info("----学院员工------");
        for (CollegeEmployee e : list1) {
            log.info(e.getId());
        }
        List<Employee> list2 = this.getAllEmployee();
        log.info("-----学校总部员工--------");
        for (Employee e : list2) {
            log.info(e.getId());
        }

    }
}


